package pub.guoxin.oops.manager.handler.impl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import pub.guoxin.oops.common.sms.enums.SmsTemplate;
import pub.guoxin.oops.enums.ComparisonOperator;
import pub.guoxin.oops.manager.handler.AbstractTwoBoundaryAlarmInfoHandler;
import pub.guoxin.oops.manager.vo.AlarmCacheVo;
import pub.guoxin.oops.model.AlarmInfo;
import pub.guoxin.oops.model.Communication;
import pub.guoxin.oops.model.CommunicationData;
import pub.guoxin.oops.model.DeviceDataInfo;

import java.util.List;

import static pub.guoxin.oops.manager.handler.AlarmFieldNameConstants.AlarmInfoHandler;
import static pub.guoxin.oops.manager.handler.AlarmFieldNameConstants.NORM_MOMENT_FLOW;

@Slf4j
@Service(NORM_MOMENT_FLOW + AlarmInfoHandler)
public class NormMomentFlowAlarmInfoHandlerImpl extends AbstractTwoBoundaryAlarmInfoHandler {

    @Override
    public List<AlarmInfo> handleRequest(CommunicationData communicationData, List<AlarmInfo> alarmInfos) {
        Communication communication = communicationData.getCommunication();
        List<DeviceDataInfo> deviceDataInfos = communicationData.getDeviceDataInfos();

        String communicationSn = communication.getSn();
        deviceDataInfos.forEach(deviceDataInfo -> {
            String deviceSn = deviceDataInfo.getSn();
            Float normMomentFlow = deviceDataInfo.getNormMomentFlow();
            AlarmCacheVo lessCache = getAlarmInfo(communicationSn, deviceSn, ComparisonOperator.LESS_THAN, NORM_MOMENT_FLOW);
            AlarmCacheVo greaterCache = getAlarmInfo(communicationSn, deviceSn, ComparisonOperator.GREATER_THAN, NORM_MOMENT_FLOW);
            ComparisonOperator comparisonOperator = judgmentBoundaryValue(normMomentFlow, NORM_MOMENT_FLOW);
            switch (comparisonOperator) {
                case NORMAL:
                    dataNormal(lessCache, communicationSn, deviceSn, ComparisonOperator.LESS_THAN, NORM_MOMENT_FLOW);
                    dataNormal(greaterCache, communicationSn, deviceSn, ComparisonOperator.GREATER_THAN, NORM_MOMENT_FLOW);
                    break;
                case GREATER_THAN:
                    deviceSnGreaterThanOrLessThan(greaterCache, communicationSn, deviceSn, normMomentFlow, NORM_MOMENT_FLOW, SmsTemplate.DEVICE_NORM_MOMENT_FLOW, ComparisonOperator.GREATER_THAN);
                    break;
                case LESS_THAN:
                    deviceSnGreaterThanOrLessThan(lessCache, communicationSn, deviceSn, normMomentFlow, NORM_MOMENT_FLOW, SmsTemplate.DEVICE_NORM_MOMENT_FLOW, ComparisonOperator.LESS_THAN);
                    break;
            }

        });

        if (this.isNextHandler()) {
            this.getSuccessor().handleRequest(communicationData, alarmInfos);
        }
        return alarmInfos;
    }

}
